package hp.com.servlet.UsersServlet;

import hp.com.entity.Users;
import hp.com.service.UsersService;
import hp.com.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/checkPhoneUpdateServlet")
public class CheckPhoneUpdateServlet extends HttpServlet {
    private UsersService usersService = new UsersServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = 0;
        try{
            id = ((Users)(req.getSession().getAttribute("userSession"))).getId();
        }catch (Exception e){
            resp.getWriter().print("-1");
            resp.getWriter().close();
        }
        String newphone = req.getParameter("newphone");
        Users newUsers = new Users();
        newUsers.setId(id);
        newUsers.setPhone(newphone);
        boolean b2 = false;
        try{
            b2 = usersService.updateUserPhone(newUsers);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (b2) {
            resp.getWriter().print(true);
            resp.getWriter().close();
        } else {
            resp.getWriter().print(false);
            resp.getWriter().close();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
